/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import com.cleangwt.uibinder.client.TemplateSupport;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The grid panel integrating bootstrap's grid system to arrange widget.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class GridPanel extends ComplexPanel {

  /**
   * The row builder.
   */
  public static class RowBuilder {
    private GridPanel panel;
    private Element row;
    private Element column;

    private RowBuilder(GridPanel panel, Element row) {
      this.panel = panel;
      this.row = row;
    }

    public RowBuilder addColumn(Widget w) {
      column = Document.get().createDivElement();
      row.appendChild(column);
      panel.add(w, column);
      return this;
    }

    /**
     * Sets the div.
     * 
     * @param lg the div for large screen, 0 to ignore
     * @param md the div for midium screen, 0 to ignore
     * @param sm the div for small screen, 0 to ignore
     * @param xs the div for extra small screen, 0 to ignore
     * @return this
     */
    public RowBuilder div(int lg, int md, int sm, int xs) {
      GridSystem.setDiv(column, lg, md, sm, xs);
      return this;
    }

    /**
     * Sets the offset.
     * 
     * @param lg the offset for large screen, 0 to ignore
     * @param md the offset for midium screen, 0 to ignore
     * @param sm the offset for small screen, 0 to ignore
     * @param xs the offset for extra small screen, 0 to ignore
     * @return this
     */
    public RowBuilder offset(int lg, int md, int sm, int xs) {
      GridSystem.setOffset(column, lg, md, sm, xs);
      return this;
    }
  }

  /**
   * Creates a grid panel that wraps an existing &lt;div&gt; element of template.
   * 
   * @param template the template
   * @param element the element to be wrapped
   */
  public static GridPanel wrap(TemplateSupport template, Element element) {
    GridPanel widget = new GridPanel(element);
    template.wrap(widget);
    return widget;
  }

  /**
   * Creates a grid panel.
   */
  public GridPanel() {
    setElement(Document.get().createDivElement());
  }

  /**
   * This constructor may be used by subclasses to explicitly use an existing element. This element
   * must be a &lt;div&gt; element.
   * 
   * @param element the element to be used
   */
  protected GridPanel(Element element) {
    setElement(element);
  }

  @SuppressWarnings("deprecation")
  @Override
  public void add(Widget w) {
    this.add(w, getElement());
  }

  /**
   * Adds a column with the given widget.
   * 
   * @param w the widget to add
   * @return the {@link RowBuilder} to build next column
   */
  public RowBuilder addColumn(Widget w) {
    Element row = Document.get().createDivElement();
    this.getElement().appendChild(row);
    return new RowBuilder(this, row).addColumn(w);
  }

}